import user from '@/api/sys/user'

export const state = () => ({
  // 登录用户信息
  userInfo: {},
  token: '',
  roles: [],
  permissions: [],
  routes: [],
  routeMap: {},
  routeMenus: [],
  menus: [],
  menu: {},
  menuIndex: '1'
})

export const mutations = {
  // 更新用户信息
  SET_USER_INFO: (state, userInfo) => {
    state.userInfo = Object.assign(state.userInfo, userInfo)
  },
  CLEAR_USER_INFO: (state) => {
    state.userInfo = {}
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_PERMISSIONS: (state, permissions) => {
    state.permissions = permissions
  },
  SET_ROUTES: (state, routes) => {
    state.routes = routes
  },
  INIT_ROUTE_MENUS: (state) => {
    state.routeMenus = state.routes[0].children
  },
  SET_ROUTE_MAP: (state, route) => {
    state.routeMap[route.componentName] = route.permission
  },
  SET_ROUTE_MENUS: (state, routeMenus) => {
    state.routeMenus = routeMenus
  },
  SET_MENUS: (state, menus) => {
    state.menus = menus
  },
  SET_MENU: (state, menu) => {
    state.menu = menu
  },
  SET_MENU_INDEX: (state, menuIndex) => {
    state.menuIndex = menuIndex
  },
  LOGOUT: (state) => {
    state.userInfo = {}
    state.token = ''
    state.roles = []
    state.permissions = []
    state.routes = []
    state.routeMap = {}
    state.routeMenus = []
    state.menus = []
    state.menu = {}
    state.menuIndex = '1'
  }
}

export const actions = {
  // 全局存放用户登录信息
  getCaptcha ({ commit }) {
    return new Promise((resolve, reject) => {
      user.getCaptcha().then((res) => {
        resolve(res)
      }).catch((err) => {
        reject(err)
      })
    })
  },
  getForgetSMS ({ commit }, phone) {
    return new Promise((resolve, reject) => {
      const data = {
        type: 'FORGET_PASSWD',
        phone
      }
      user.getSMS(data).then((res) => {
        resolve(res)
      }).catch((err) => {
        reject(err)
      })
    })
  },
  loginByUserName ({ commit }, data) {
    return new Promise((resolve, reject) => {
      user.login(data).then((res) => {
        // 如果登录成功
        const resCode = res.code
        if (resCode === 0) {
          const resData = res.data
          commit('SET_TOKEN', resData.token)
          commit('SET_USER_INFO', resData.user)
          commit('SET_ROLES', resData.user.roles)
          commit('SET_PERMISSIONS', resData.user.permissions)
        }
        resolve(res)
      }).catch((err) => {
        reject(err)
      })
    })
  },
  // 根据手机号登录
  LoginByPhone ({ commit }, data) {
    return new Promise((resolve, reject) => {
      user.login(data).then((res) => {
        // 如果登录成功
        const resCode = res.code
        if (resCode === 0) {
          const resData = res.data
          commit('SET_TOKEN', resData.token)
          commit('SET_USER_INFO', resData.user)
          commit('SET_ROLES', resData.user.roles)
          commit('SET_PERMISSIONS', resData.user.permissions)
        }
        resolve(res)
      }).catch((err) => {
        reject(err)
      })
    })
  },
  clearUserInfo ({ commit }) {
    commit('CLEAR_USER_INFO')
  },
  updateUserInfo ({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      commit('SET_USER_INFO', userInfo)
      resolve(userInfo)
    })
  },
  // 获取用户信息
  getUserInfo ({ commit }) {
    return new Promise((resolve, reject) => {
      user.getUserInfo().then((res) => {
        const resCode = res.code
        if (resCode === 0) {
          const resData = res.data
          commit('SET_TOKEN', resData.token)
          commit('SET_USER_INFO', resData.user)
          commit('SET_ROLES', resData.user.roles)
          commit('SET_PERMISSIONS', resData.user.permissions)
        }
        resolve(res)
      }).catch((err) => {
        reject(err)
      })
    })
  },
  // 注册
  register ({ commit }, data) {
    return new Promise((resolve, reject) => {
      user.register(data).then((res) => {
        const resCode = res.code
        if (resCode === 0) {
          const resData = res.data
          commit('SET_TOKEN', resData.token)
          commit('SET_USER_INFO', resData.user)
          commit('SET_ROLES', resData.user.roles)
          commit('SET_PERMISSIONS', resData.user.permissions)
        }
        resolve(res)
      }).catch((err) => {
        reject(err)
      })
    })
  },
  // 检查用户是否存在
  userVerify ({ commit }, data) {
    return new Promise((resolve, reject) => {
      user.userVerify(data).then((res) => {
        resolve(res)
      }).catch((err) => {
        reject(err)
      })
    })
  },
  forgetPwd ({ commit }, data) {
    return new Promise((resolve, reject) => {
      user.forgetPwd(data).then((res) => {
        resolve(res)
      }).catch((err) => {
        reject(err)
      })
    })
  },
  forgetPwdVerify ({ commit }, data) {
    return new Promise((resolve, reject) => {
      user.forgetPwdVerify(data).then((res) => {
        resolve(res)
      }).catch((err) => {
        reject(err)
      })
    })
  },
  // 登出
  logout ({ commit }) {
    return new Promise((resolve, reject) => {
      commit('LOGOUT')
      commit('app/CLEAR_LOCK', null, { root: true })
      commit('log/CLEAR_LOG', null, { root: true })
      commit('app/DEL_ALL_VISITED_VIEWS', null, { root: true })
      commit('app/DEL_ALL_CACHED_VIEWS', null, { root: true })
      user.logout().then((res) => {
        resolve(res)
      }).catch((err) => {
        reject(err)
      })
    })
  },
  // ---------------------------------------------后台操作接口
  GetTopMenu ({ commit, state }) {
    return new Promise((resolve) => {
      // 采用前端控制,有问题,第一次加载的时候,可能会出现router没有获取不能显示
      // const menus = state.routes
      // commit('SET_MENUS', menus)
      // commit('INIT_ROUTE_MENUS')
      // resolve(menus)
      // 后端控制
      user.getTopMenu().then((res) => {
        const menus = res.data || []
        commit('SET_MENUS', menus)
        resolve(menus)
      })
    })
  },
  GetRouter ({ commit }) {
    return new Promise((resolve) => {
      user.getMenu().then((res) => {
        const routes = res.data || []
        commit('SET_ROUTES', routes)
        commit('INIT_ROUTE_MENUS')
        // 设置对应的后面用于鉴权的routeMap
        function addRouteMap (routes) {
          routes.forEach((r) => {
            commit('SET_ROUTE_MAP', r)
            if (r.children) {
              addRouteMap(r.children)
            }
          })
        }
        addRouteMap(routes)
        resolve(routes)
      })
    })
  },
  // 不再使用
  GetChildMenu ({ commit }, data) {
    commit('SET_CHILD_MENUS', data.id)
    return new Promise((resolve) => {
      user.getChildMenu(data).then((res) => {
        const routers = res.data || []
        resolve(routers)
      })
    })
  },
  setMenu ({ commit }, menu) {
    commit('SET_MENU', menu)
  },
  setMenuIndex ({ commit }, menuIndex) {
    commit('SET_MENU_INDEX', menuIndex)
  },
  setRouteMenus ({ commit }, routeMenus) {
    commit('SET_ROUTE_MENUS', routeMenus)
  },
  // 添加
  addUser ({ state, commit }, data) {
    return new Promise((resolve, reject) => {
      user.addUser(data).then((res) => {
        resolve(res)
      }).catch((err) => {
        reject(err)
      })
    })
  },
  // 更新
  updateUser ({ state, commit }, data) {
    return new Promise((resolve, reject) => {
      user.updateUser(data).then((res) => {
        resolve(res)
      }).catch((err) => {
        reject(err)
      })
    })
  },
  // 删除
  removeUserById ({ state, commit }, data) {
    return new Promise((resolve, reject) => {
      user.removeUserById(data).then((res) => {
        resolve(res)
      }).catch((err) => {
        reject(err)
      })
    })
  },
  // 删除
  removeUsersById ({ state, commit }, data) {
    return new Promise((resolve, reject) => {
      user.removeUsersById(data).then((res) => {
        resolve(res)
      }).catch((err) => {
        reject(err)
      })
    })
  },
  // 通过Id单独查询列表
  getUserById ({ state, commit }, data) {
    return new Promise((resolve, reject) => {
      user.getUserById(data).then((res) => {
        resolve(res)
      }).catch((err) => {
        reject(err)
      })
    })
  },
  // 查询全部结果列表
  getUserList ({ state, commit }, data) {
    return new Promise((resolve, reject) => {
      user.getUserList(data).then((res) => {
        resolve(res)
      }).catch((err) => {
        reject(err)
      })
    })
  },
  // 分页查询结果
  getUserPageList ({ state, commit }, data) {
    return new Promise((resolve, reject) => {
      user.getUserPageList(data).then((res) => {
        resolve(res)
      }).catch((err) => {
        reject(err)
      })
    })
  },
  // 重置用户密码
  resetUserPasswdBatch ({ state, commit }, data) {
    return new Promise((resolve, reject) => {
      user.resetUserPasswdBatch(data).then((res) => {
        resolve(res)
      }).catch((err) => {
        reject(err)
      })
    })
  },
  getUserRoleByUserId ({ state, commit }, data) {
    return new Promise((resolve, reject) => {
      user.getUserRoleByUserId(data).then((res) => {
        resolve(res)
      }).catch((err) => {
        reject(err)
      })
    })
  },
  lockUser ({ state, commit }, data) {
    return new Promise((resolve, reject) => {
      user.lockUser(data).then((res) => {
        resolve(res)
      }).catch((err) => {
        reject(err)
      })
    })
  }
}
